<?php
require_once APPPATH .'models/base_model.php';
require_once 'user_model.php';

class Product_Model extends Base_Model {
	
	const STATE_AVAILABLE = 'AVAILABLE';
	const STATE_GIVEN = 'GIVEN';
	const STATE_UNKNOWN = 'UNKNOWN';
	
	const LIST_ORDER_LATEST = 'latest';
	const LIST_ORDER_RANDOM = 'random';
	
	public function save($data) {
		$data['state'] = self::STATE_AVAILABLE;
		$this->db->insert('products', $data);
		return $this->db->insert_id();
	}
	
	public function exist($id) {
		$r = $this->db
			->select('id')
			->get_where('products', array('id' => $id))
			->row_array();
		
		return !empty($r);
	}
	
	public function save_image($id, $image_name) {
		$this->db->insert('products_images', array(
				'product_id' => $id,
				'name' => $image_name));
		return $this->db->insert_id();
	}
	
	/**
	 * Save a comment into product comments database table by user_id.
	 * @param int $user_id
	 * @param string $comment
	 */
	public function save_comment($user_id, $product_id, $comment) {
		$this->db->insert('products_comments', array(
				'comment' => $comment,
				'product_id' => $product_id,
				'user_id' => $user_id));
		return $this->db->insert_id();
	}
	
	public function remove($id) {
		return $this->db
			->where(array('id' => $id))
			->delete('products');
	}
	
	public function get($id) {
		return $this->db
			->select('p.*, pc.name AS category_name')
			->from('products AS p')
			->join('products_categories AS pc', 'p.category_id = pc.id')
			->get_where('products', array('p.id' => $id), 1)
			->row_array();
	}
	
	public function get_images($id) {
		return $this->db
			->get_where('products_images', array('product_id' => $id))
			->result_array();
	}
	
	public function get_comments($id) {
		return $this->db
			->select('pc.*, u.name AS user_name')
			->from('products_comments AS pc')
			->join('users AS u', 'pc.user_id = u.id')
			->where(array('pc.product_id' => $id))
			->order_by('pc.creation_time', 'desc')
			->get()->result_array();
	}
	
	public function get_categories() {
		return $this->db
			->get('products_categories')->result_array();
	}
	
	/**
	 * Return a list of products by ordering type and with paging functionality.
	 * The function also extract a name of one of the names of images.
	 * @param string $order_type
	 * @param int $page
	 * @param int $count
	 */
	public function get_products($order_type = self::LIST_ORDER_LATEST, $page = 0, $count = 50) {
		switch ($order_type) {
			case self::LIST_ORDER_LATEST:
				$order = 'ORDER BY `creation_time` DESC';
				break;
				
			case self::LIST_ORDER_RANDOM:
				$order = 'ORDER BY RAND()';
				break;
				
			default: $order = '';
		}
		
		$query = 'SELECT 
				SQL_CALC_FOUND_ROWS *,
				(SELECT name FROM products_images WHERE product_id = p.id LIMIT 1) AS image_name
			FROM products AS p ' 
			. $order
			.' LIMIT '. $page * $count .' , '. $count;
		
		return $this->get_paged_data($query, $page, $count);
	}
}